package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.jstyle.jclife.R;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.ImageUtils;
import com.jstyle.jclife.utils.SharedPreferenceUtils;
import com.jstyle.jclife.view.HeadCircleView;

import java.math.BigDecimal;
import java.text.NumberFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/11.
 */

public class UserInfoAdapter extends RecyclerViewBaseAdapter<UserInfo> {


    final int VIEWTYPE_NORMAL = 0;
    final int VIEWTYPE_OTHER = 1;
    final int VIEWTYPE_unit = 2;
    UserInfo userInfo;
    String[] titles;
    String[] weightLabel;
    String[] heightLabel;


    public UserInfoAdapter(UserInfo userInfo, Context context) {
        super(userInfo);
        this.userInfo = userInfo;

        weightLabel = context.getResources().getStringArray(R.array.weight_labels);
        heightLabel = context.getResources().getStringArray(R.array.height_labels);
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new NormalViewHolder(view);
                break;
            case VIEWTYPE_OTHER:
                View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new IconViewHolder(viewOther);
                break;
            case VIEWTYPE_unit:
                View unit = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new UnitViewHolder(unit);
                break;
        }
        return baseViewHolder;
    }


    public class NormalViewHolder extends BaseViewHolder {
        @BindView(R.id.bt_setting_position)
        Button btSettingPosition;
        @BindView(R.id.view_space)
        View viewSpace;
        @BindView(R.id.bt_setting_right)
        Button btSettingRight;

        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class IconViewHolder extends BaseViewHolder {

        @BindView(R.id.iv_user)
        ImageView iv_icon;
        @BindView(R.id.bt_icon_right)
        Button btSettingRight;

        public IconViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class UnitViewHolder extends BaseViewHolder {
        @BindView(R.id.RadioGroup_weight)
        RadioGroup RadioGroupWeight;
        @BindView(R.id.RadioGroup_height)
        RadioGroup RadioGroupHeight;

        public UnitViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    protected void bindData(BaseViewHolder holder, final int position) {

        switch (holder.getItemViewType()) {
            case VIEWTYPE_NORMAL:
                bindNormal(holder, position);
                break;
            case VIEWTYPE_OTHER:
                bindIcon(holder, position);
                break;
            case VIEWTYPE_unit:
                bindUnit(holder);
                break;
        }

    }

    private void bindIcon(BaseViewHolder holder, int position) {
        IconViewHolder iconViewHolder = (IconViewHolder) holder;
        iconViewHolder.btSettingRight.setText(titles[position]);
        Context context = iconViewHolder.itemView.getContext();
        if(userInfo==null)return;
        Uri iconUri = ImageUtils.getIconUri(context, userInfo.getUserId());
        Bitmap bmHead = ImageUtils.findHeadBitmap(context, iconUri);
        bmHead = bmHead == null ? BitmapFactory.decodeResource(context.getResources(),
                R.drawable.user_icon) : bmHead;
        HeadCircleView circleView = new HeadCircleView(bmHead);
        iconViewHolder.iv_icon.setImageDrawable(circleView);
    }

    private void bindNormal(BaseViewHolder holder, final int position) {
        NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
        setUserInfo(position, normalViewHolder);
    }
    private void bindUnit(BaseViewHolder holder){
        UnitViewHolder unitViewHolder=(UnitViewHolder)holder;
        int weightUnit= SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_UNIT_WEIGHT,0);
        int heightUnit= SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_UNIT_HEIGHT,0);
        unitViewHolder.RadioGroupWeight.check(weightUnit==0?R.id.RadioButton_kg:R.id.RadioButton_lb);
        unitViewHolder.RadioGroupHeight.check(heightUnit==0?R.id.RadioButton_cm:R.id.RadioButton_ft);
        unitViewHolder.RadioGroupHeight.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int height=0;
                if(checkedId==R.id.RadioButton_cm){
                    height=0;
                }else  if(checkedId==R.id.RadioButton_ft){
                    height=1;
                }
                SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_UNIT_HEIGHT,height);
                notifyDataSetChanged();
            }
        });
        unitViewHolder.RadioGroupWeight.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int weight=0;
                if(checkedId==R.id.RadioButton_kg){
                    weight=0;
                }else if(checkedId==R.id.RadioButton_lb){
                    weight=1;
                }
                SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_UNIT_WEIGHT,weight);
                notifyDataSetChanged();
            }
        });

    }



    public void setTitles(String[] titles) {
        this.titles = titles;
    }

    private static final String TAG = "UserInfoAdapter";
    private void setUserInfo(int position, NormalViewHolder normalViewHolder) {
        if(userInfo==null)return;;
        String text = titles[position];
        String info = "";
        Context context = normalViewHolder.itemView.getContext();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);

        nf.setGroupingUsed(false);
        switch (position) {
            case 1:
                info = userInfo.getName();
                break;
            case 2:
                info = userInfo.getGender() == 0 ? context.getString(R.string.male) : context.getString(R.string.female);
                break;
            case 4:
                int heightUnit= SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_UNIT_HEIGHT,0);
                int height=userInfo.getHeight();
                if(heightUnit==1){
                    float  foot = height / 100.0f / 0.3048f;
                    int intFloot=(int)foot;
                    float inch = (height / 100.0f/ 0.3048f - intFloot) * 12;
                    BigDecimal bigDecimal=new BigDecimal(inch);
                    BigDecimal bigDecimal2=bigDecimal.setScale(0,BigDecimal.ROUND_HALF_UP);
                    info=(int)foot+"feet,"+bigDecimal2.intValue()+"inches";
                }else{
                    info=height+"cm";
                }

                break;
            case 5:
                int weightUnit=SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_UNIT_WEIGHT,0);
                int weight=userInfo.getWeight();
                if(weightUnit==1){
                    Log.i(TAG, "setUserInfo:w "+weight/0.454f);
                    BigDecimal bigDecimaWeight=new BigDecimal(weight/0.454f);
                    BigDecimal bigDecimal2=bigDecimaWeight.setScale(0,BigDecimal.ROUND_HALF_UP);
                    weight= bigDecimal2.intValue();
                }
                info = weight + weightLabel[weightUnit];
                break;
            case 6:
                info = userInfo.getBirthday();

                break;
            case 7:
                double bmi=userInfo.getWeight()/Math.pow(userInfo.getHeight()/100f,2);
                nf.setMaximumFractionDigits(1);
                info=nf.format(bmi);
                break;
        }
        normalViewHolder.viewSpace.setVisibility(position == 7 ? View.GONE : View.VISIBLE);
        normalViewHolder.btSettingPosition.setText(text);
        normalViewHolder.btSettingRight.setText(info);
        normalViewHolder.btSettingRight.setVisibility(View.VISIBLE);

    }

    @Override
    public int getItemViewType(int position) {
        int type = VIEWTYPE_NORMAL;
        switch (position) {
            case 0:
                type = VIEWTYPE_OTHER;
                break;
            case 3:
                type = VIEWTYPE_unit;
                break;
        }
        return type;
    }

    /**
     * @param viewType
     * @return
     */
    @Override
    protected int getLayoutId(int viewType) {
        int id = R.layout.item_userinfo;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                id = R.layout.item_userinfo;
                break;
            case VIEWTYPE_OTHER:
                id = R.layout.item_userinfo_icon;
                break;
            case VIEWTYPE_unit:
                id = R.layout.item_userinfo_unit;
        }
        return id;
    }

    @Override
    public int getItemCount() {
        return 8;
    }
}
